<?php
namespace Seller\Model;
use Think\Model;
class PhpexcelModel extends Model {

    //php_excel对象
    private $obj;
    private $write;

    public function __construct() {
        vendor("PHPExcel.Classes.PHPExcel");
        $this->obj = new \PHPExcel();
        $this->write = new \PHPExcel_Writer_Excel5($this->obj);
    }

    //设置表的基本格式
    public function setStyle($set_width=20, $set_size=14, $range="A1:AG1") {
        //设置第一行的宽度和字体的大小
        $this->obj->getActiveSheet()->getDefaultColumnDimension()->setWidth($set_width);
        $this->obj->getActiveSheet()->getStyle($range)->getFont()->setSize($set_size);
    }

    //生成表格
    public function create($header, $data) {
        //生成主体部分
        $keys = array();
        foreach ($data as $key => $value) {

            //把key存入数组中
            foreach ($value as $k =>$v) {array_push($keys, $k);}
            break;


            foreach ($keys as $vv) {
                $i=$key+1;
                $letter = chr($key+1+64);

                //$this->obj->getActiveSheet()->setCellValue("$letter".$i,  $value[$vv]);
                echo "$letter".$i;
                echo '<br/>';
                echo $value[$vv];
                echo '<hr/>';


            }





        }







        exit;
        $this->outPut('订单');
    }

    //输出
    public function outPut($name) {
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename=订单.xls');
        header("Content-Transfer-Encoding:binary");
        $this->write->save($name.'.xls');
        $this->write->save('php://output');
    }


}

